﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml.Linq;
using System.Xml.Serialization;
using Sharp.Framework.Extensions;

namespace LERP.Web.App_Start
{
    public class FunctionConfigManger
    { 

        private static FunctionConfig _config;
        private static List<FunctionItemAction> _allActions;
        /// <summary>
        /// 配置
        /// </summary>
        public static FunctionConfig Instance
        {
            get
            {
                if (null == _config)
                {
                    var configString = XDocument.Load(HttpContext.Current.Server.MapPath("~/App_Data/function.xml")).ToString();
                    _config = Serialization.XmlToObject<FunctionConfig>(configString);
                }
                return _config;
            }
        }


        /// <summary>
        /// 受限的Action
        /// </summary>
        public static List<FunctionItemAction> AllActions
        {
            get
            {
                if (null == _allActions)
                {
                    _allActions = new List<FunctionItemAction>();
                    foreach (var item in Instance.Items)
                    {
                        _allActions.AddRange(item.Actions);
                    }

                }
                return _allActions;
            }
        }
        /// <summary>
        /// 受限的Action
        /// </summary>
        public static List<FunctionItemAction> GetActions()
        {
            var result = new List<FunctionItemAction>();
            foreach (var item in Instance.Items)
            {
                result.AddRange(item.Actions);
            }

            return result;
        }

        /// <summary>
        /// 可访问的Action
        /// </summary>
        public static List<FunctionItemAction> GetActions(int functionId)
        {
            var result = new List<FunctionItemAction>();
            if (functionId != 0)
            {
                foreach (var item in Instance.Items)
                {
                    if ((item.Id & functionId) == item.Id)
                    {
                        result.AddRange(item.Actions);
                    }
                }

            }
            return result;
        }

    }
    [System.Serializable]
    [XmlRoot("functions", Namespace = "")]
    public class FunctionConfig
    {
        [XmlElement("item")]
        public List<FunctionItem> Items { get; set; }
    }

    [System.Serializable]
    [XmlRoot("item", Namespace = "")]
    public class FunctionItem
    {

        [XmlAttribute("id")]
        public int Id { get; set; }

        [XmlAttribute("name")]
        public string Name { get; set; }
        [XmlElement("action")]
        public List<FunctionItemAction> Actions { get; set; }
    }

    [System.Serializable]
    [XmlRoot("action", Namespace = "")]
    public class FunctionItemAction
    {

        [XmlAttribute("contrller")]
        public string Contrller { get; set; }

        [XmlAttribute("name")]
        public string Name { get; set; }
        [XmlAttribute("method")]
        public string Method { get; set; }
    }
}